package com.jinke.uang.common;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.jinke.uang.AppApplication;
import com.jinke.uang.cache.Acache;
import com.jinke.uang.cache.AcacheKeys;
import com.jinke.uang.bean.AuthData;
import com.jinke.uang.utils.StringUtils;

public class UserInfo {

    public static boolean isLogin() {
        if (!StringUtils.isEmpty(getToken())) {
            return true;
        }
        return false;
    }

    public static void setToken(String token) {
        Acache.get(AppApplication.getContext()).put(AcacheKeys.TOKEN, token);
    }


    public static String getToken() {
        String token = Acache.get(AppApplication.getContext()).getAsString(AcacheKeys.TOKEN);
       //测试
//         token = "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxIiwidXNlcklkIjoiMTY2NTYiLCJuYW1lIjoiMDEwMSIsImNoYW5uZWxOYW1lIjoiQW5kcm9pZCIsImV4cCI6MTU0MTc0NzE3OX0.jw4VKerOsnhAqpEOnEG-05PCduB-5eEciJdeIxq9FcuB9efb2akRM9Pa3k4dSVyb7NAeWxoo8ZpeIfIcZkpTsmrgunjfjE_VqSw0sX_mmdraCJl-BAs9z_zTOrPMi5hUQC808Djj9Ib91pqPNdcaEgku_oPNhw5Tlm27U2xSi2U";
       //开发
//        token="eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxIiwidXNlcklkIjoiMSIsIm5hbWUiOiIwMSIsImNoYW5uZWxOYW1lIjoiQW5kcm9pZCIsImV4cCI6MTU0MTQ3NTUxNn0.ag0zO-McMGZafzaTReORVtkNKJXICQscxC2WIms8pz4lpyHWXMrkiUgqNH5pGkFdK9llp-dMwOhPDkS8M2XXDCRQQdIShCjNYRFTkk80qo6cET3nD6GpPYMzyJ6TwYfy8rFxCWcwmy7vQVg7WSK-cossReX6wy3L9v9jNKDxj6g";
        if (TextUtils.isEmpty(token)) token = "";
        return token;
    }


    public static void logout() {
        Acache.get(AppApplication.getContext()).remove(AcacheKeys.TOKEN);
    }


    public static String getSaveUuid() {
        String auth_data = Acache.get(AppApplication.getContext()).getAsString(AcacheKeys.AUTH);
        String backUuid = Acache.get(AppApplication.getContext()).getAsString(AcacheKeys.BACK);
        if(!TextUtils.isEmpty(backUuid)){
            return backUuid;
        }
        else if (!TextUtils.isEmpty(auth_data)) {
            Gson gson = new Gson();
            AuthData authData = gson.fromJson(auth_data, AuthData.class);
            return authData.getUserUuid();
        }

        return "";
    }

    /**
     * 是否回显
     * @return
     */
    public static boolean isShowUserInformation() {
        String is_back = Acache.get(AppApplication.getContext()).getAsString(AcacheKeys.BACK);
        String auth_data = Acache.get(AppApplication.getContext()).getAsString(AcacheKeys.AUTH);
        if (!TextUtils.isEmpty(is_back)) {
            return true;
        } else if (!TextUtils.isEmpty(auth_data)) {
            Gson gson = new Gson();
            AuthData authData = gson.fromJson(auth_data, AuthData.class);
            return authData.getIsEcho() == 2;
        }

        return false;
    }

    /**
     * 获取uuid
     * @return
     */
    public static String getShowUuid() {
        String backUuid = Acache.get(AppApplication.getContext()).getAsString(AcacheKeys.BACK);
        String auth_data = Acache.get(AppApplication.getContext()).getAsString(AcacheKeys.AUTH);
        if(!TextUtils.isEmpty(backUuid)){
            return backUuid;
        }
        else if (!TextUtils.isEmpty(auth_data)) {
            Gson gson = new Gson();
            AuthData authData = gson.fromJson(auth_data, AuthData.class);
            if (isShowUserInformation()) {
                return authData.getPreviousUserUuid();
            }
            return authData.getUserUuid();
        }

        return "";
    }

    /**
     * 是否资料退回的
     * @return
     */
    public static boolean isUserInformationWrong() {
        String isBack = Acache.get(AppApplication.getContext()).getAsString(AcacheKeys.BACK);
        if (TextUtils.isEmpty(isBack)){
            return false;
        }
        return true;
    }
}
